package com.mm.ai.common;

import com.mm.ai.common.dto.ResponseEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author：tangnaichao
 * @Date：2024/10/15 19:28
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity globalExceptionHandler(HttpServletRequest request, Exception ex) throws Exception {
        if (ex != null) {
            logger.error("GlobalExceptionHandler:" + ex.getMessage(), ex);
        }
        ResponseEntity retDTO = new ResponseEntity();
        retDTO.setSuccess(false);
        retDTO.setResultCode("500");
        if (ex instanceof CommonException) {
            retDTO.setResultMessage(ex.getMessage());
            return retDTO;
        }
        return retDTO;
    }
}
